<template>
  <view class="index">
    <view class="center">
      <view class="label">请设置出生日期</view>
      <view class="ind_from">
        <view class="row">
          <picker
            class="birth"
            mode="date"
            :value="birth"
            start="1900-01-01"
            @change="changeBirthChange"
          >
            <view class="uni-input">{{
              birth ? birth : "请设置出生日期"
            }}</view>
          </picker>
        </view>
      </view>
      <view class="btn" @click="submit">开始计算</view>
      <view v-if="showResult" class="result">
        <view class="desc"
          >周岁：<text class="strong">{{ result.zhouDate }}</text
          >岁</view
        >
        <view class="desc"
          >虚岁：<text class="strong">{{ result.xuDate }}</text
          >岁</view
        >
      </view>
      <!-- <view class="sub_text">
        <view class="p red">温馨提示：以上信息仅供参考！</view>
      </view> -->
      <view class="pub_ad_banner">
        <!-- #ifdef MP-TOUTIAO -->
        <ad
          :unit-id="adBannerIdDouyin"
          :ad-intervals="30"
          :fixed="false"
          type="banner"
          scale="100"
        />
        <!-- #endif -->
        <!-- #ifdef MP-KUAISHOU -->
        <ad :type="100011054" :unit-id="adBannerIdKuaishou"></ad>
        <!-- #endif -->
      </view>
    </view>
    <tabBar></tabBar>
  </view>
</template>

<script>
import {
  showAdConfirmModal,
  adVideoCreate,
  adVideoShow,
  adBannerIdDouyin,
  adBannerIdKuaishou,
} from "@/common/ad";
export default {
  data() {
    return {
      adBannerIdDouyin: adBannerIdDouyin,
      adBannerIdKuaishou: adBannerIdKuaishou,
      banner: null,
      birth: "1980-01-01",
      timer: null,
      showResult: false,
      isUnlock: false,
      result: {
        zhouDate: "",
        xuDate: "",
      },
    };
  },
  onLoad(option) {
    adVideoCreate();
  },
  methods: {
    changeBirthChange(e) {
      clearTimeout(this.timer);
      this.birth = e.detail.value;
      this.showResult = false;
    },
    // 周岁
    calculateAge(birthYear, birthMonth, birthDay) {
      var today = new Date();
      var age = today.getFullYear() - birthYear;
      if (
        today.getMonth() < birthMonth - 1 ||
        (today.getMonth() === birthMonth - 1 && today.getDate() < birthDay)
      ) {
        age--;
      }
      return age;
    },
    // 虚岁
    calculateChineseAge(birthYear, birthMonth, birthDay) {
      var age = this.calculateAge(birthYear, birthMonth, birthDay);
      var today = new Date();
      var lunarNewYearMonth = 1;
      var lunarNewYearDay = 1;
      if (
        today.getMonth() < lunarNewYearMonth ||
        (today.getMonth() === lunarNewYearMonth &&
          today.getDate() < lunarNewYearDay)
      ) {
        return age;
      } else {
        return age + 1;
      }
    },
    tranTime(date) {
      var date = new Date(this.birth);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      this.result.zhouDate = this.calculateAge(year, month, day);
      this.result.xuDate = this.calculateChineseAge(year, month, day);
    },
    getResult() {
      this.tranTime();
      this.showResult = true;
    },
    submit() {
      if (this.birth == "") {
        this.global.toast("请设置出生日期");
        return;
      }
      if (!this.isUnlock) {
        showAdConfirmModal(() => {
          try {
            adVideoShow(() => {
              this.getResult();
            });
          } catch (error) {
            this.getResult();
          }
        });
      } else {
        this.getResult();
      }
    },
  },
};
</script>

<style scoped lang="scss">
.index {
  display: flex;
  align-items: center;
  height: 100vh;
  width: 100%;
}
.center {
  width: 100%;
}
.banner {
  display: block;
  width: 100%;
}
.pub_radio {
  position: relative;
  padding-left: 50rpx;
}
.pub_radio:before {
  position: absolute;
  top: 50%;
  margin-top: -20rpx;
  left: 0;
  content: "";
  width: 40rpx;
  height: 40rpx;
  background-image: url("@/static/img/checked.png");
  background-size: cover;
}
.pub_radio.active:before {
  background-image: url("@/static/img/checked_active.png");
  background-size: cover;
}
.ind_from {
  background: #f9f5ef;
  // margin-top: 200px;
  border: 4rpx solid #b39569;
  border-radius: 10rpx;
  .row {
    padding: 16rpx 20rpx;
    display: flex;
    border-bottom: 1px solid #ccc;
    &:last-child {
      border-bottom: 0;
    }
    .label {
      width: 190px;
      flex-shrink: 0;
    }
    .value {
      width: 100%;
      display: flex;
      input {
        width: 100%;
        display: inline-block;
        margin-right: 10px;
        background: #ccc;
        text-align: center;
      }
    }
    .pub_radio {
      display: inline-block;
      margin-right: 40rpx;
    }
  }
}
.btn {
  margin-top: 30rpx;
  background: #db0000;
  color: #fff;
  text-align: center;
  padding: 14rpx 0;
  border-radius: 10rpx;
}
.sub_text {
  text-align: center;
  padding-top: 30rpx;
  font-size: 24rpx;
  color: #666;
  .red {
    color: red;
  }
  .bold {
    font-weight: bold;
  }
}
.pub_ad_banner {
  margin-top: 30rpx;
}
.label {
  text-align: center;
  margin-bottom: 10px;
}
.result {
  background: #f9f5ef;
  margin-top: 20rpx;
  border: 4rpx solid #b39569;
  border-radius: 10rpx;
  padding: 20px 10px;

  .title {
    text-align: center;
    font-size: 30px;
    margin-bottom: 10px;
  }
  .desc {
    font-size: 24px;
    text-align: center;
    .strong {
      font-size: 32px;
      color: #ff1afe;
    }
  }
}
.birth {
  margin: 0 auto;
}
</style>
